package kit.edu.lego.gruppe3.actions;

import kit.edu.lego.gruppe3.ComponentsRepository;
import lejos.robotics.navigation.DifferentialPilot;

public class Movement {
	
	private static DifferentialPilot robot = ComponentsRepository.getInstance().getRobotPilot();
	private static boolean blocked = false;
	
	public static boolean isMoving(){
		return robot.isMoving();
	}
	
	public static boolean  moveForward(double speed){
		if(blocked) return false;
		blocked = true;
		robot.setTravelSpeed(speed);
		robot.forward();
		blocked = false;
		return true;
	}
	
	public static boolean moveBackward(double speed){
		if(blocked) return false;
		blocked = true;
		robot.setTravelSpeed(speed);
		robot.backward();
		blocked = false;
		return true;
	}
	public static boolean block(){
		if(blocked) return false;
		blocked = true;
		return true;
	}
	public static boolean unblock(){
		if(!blocked) return false;
		blocked = false;
		return true;
	}

	public static boolean  travel(double distance, double speed){
		return travel(distance, speed, false);
	}

	public static boolean  travel(double distance, double speed,  boolean immediateReturn){
		if(blocked) return false;
		blocked = true;
		robot.setTravelSpeed(speed);
		robot.travel(distance,immediateReturn);
		blocked = false;
		return true;
	}
	
	
	public static boolean turnCenter(int angle){
		return turnCenter(angle, false);
	}
	public static boolean turnCenter(int angle, boolean immediateReturn){
		if(blocked) return false;
		blocked = true;
		robot.setRotateSpeed(90);
		robot.rotate(-1.03*angle,immediateReturn);
		blocked = false;
		return true;
	}
	
	
	public static boolean stop(){
		if(blocked) return false;
		robot.stop();
		return true;
		
	}

	public static DifferentialPilot getRobot() {
		return robot;
	}
}
